ihelpuoo.namespace("ihelpuoo.widget.login");

ihelpuoo.widget.login.LoginView = function(gasket
										,name
										,patternName
										,moduleName
										,actionName
										,fAfterLogin
										,nmUserName
										,nmPassword
										,nmVerify)
{
	this.name = name;
	this.view = new ihelpuoo.widget.View(gasket,(patternName?patternName:"eve.pattern.login.normal"),this.name);
	this.moduleName = moduleName;
	this.actionName = actionName;
	this.nmUserName = (nmUserName?nmUserName:"name");
	this.nmPassword = (nmPassword?nmPassword:"password");
	this.nmVerify = (nmVerify?nmVerify:"verify");
	this.fAfterLogin = fAfterLogin;
	ihelpuoo.widget.registerView2(this);	
}

ihelpuoo.widget.login.LoginView.prototype
=
{
	draw : function()
	{
		if(this.view.isNeverBeDrawn())
		{
			this.view.preparePattern();
			this.oLang = this.view.getElement("language");
			if(this.oLang)
			{
				this.vLang = new ihelpuoo.widget.language.LanguageListView(this.oLang);
			}
			ihelpuoo.widget.addViewEventByKey(this,this.nmUserName,"keyup",this.onEnter);
			//ihelpuoo.widget.addViewEventByKey(this,this.nmUserName,"keydown",this.onKeydownOnName);
			ihelpuoo.widget.addViewEventByKey(this,this.nmPassword,"keyup",this.onEnter);
			ihelpuoo.widget.addViewEventByKey(this,this.nmVerify,"keyup",this.onEnter);
			ihelpuoo.widget.addViewEventByKey(this,"btnLogin","click",this.onLogin);
			ihelpuoo.widget.addViewEventByKey(this,"btnReset","click",this.onReset);
		}
		if(this.vLang)
			this.vLang.refresh();
		this.reset();
		this.view.display();
	}
	
	,
	focusName: function()
	{
		this.view.focusElement(this.nmUserName);
	}
	
	,
	reset: function()
	{
		this.view.set(this.nmUserName,"");
		this.view.set(this.nmPassword,"");
		this.view.set(this.nmVerify,"");
		var ele = this.view.getElement(this.nmUserName);
		ihelpuoo.dom.refreshIFrame(this.view.getElement("code"));
	}
	
	,
	onLogin: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		if(v)v.login();
	}
	
	,
	onEnter: function()
	{
		var oEvent = ihelpuoo.Event(arguments);
		if(ihelpuoo.event.isEnter(oEvent))
		{
			var v = ihelpuoo.widget.getViewByEventSource(this);
			if(v)v.login();
		}
	}
	
	,
	onKeydownOnName: function()
	{
		var oEvent = ihelpuoo.Event(arguments);
		if(oEvent.keyCode==9)
		{
			var v = ihelpuoo.widget.getViewByEventSource(this);
			v.view.focusElement(this.nmPassword);
			return false;
		}
		return true;
	}
	
	,
	login: function()
	{
		var postStr = this.getPostString();
		if(ihelpuoo.isBlank(postStr))
		{
			ihelpuoo.warn("Name and password should not by null!");
			return;
		}
		var url = ihelpuoo.http.server().url(this.moduleName,this.actionName);
		if(ihelpuoo.isInfoEnabled())
			ihelpuoo.info("LOGIN:" + url + "?" + postStr);
		var xml = ihelpuoo.http.POSTXML(url,postStr);
		if(null!=xml && ihelpuoo.isObject(xml))
		{
			if(null!=this.fAfterLogin)
				this.fAfterLogin(xml);
		}
		else
		{
			this.view.set("verify","");
			ihelpuoo.dom.refreshIFrame(this.view.getElement("code"));
		}
	}
	
	,
	onReset: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		if(null!=v)
			v.reset();
	}
	
	,
	getPostString: function()
	{
		var re = "";
		var v = this.view.getString(this.nmUserName);
		if(!ihelpuoo.isBlank(v))
			re += this.nmUserName + "=" + v;
		v = this.view.getString(this.nmPassword);
		if(!ihelpuoo.isBlank(v))
			re += "&" + this.nmPassword + "=" + v;
		v = this.view.getString(this.nmVerify);
		if(!ihelpuoo.isBlank(v))
			re += "&" + this.nmVerify + "=" + v;
		return re;
	}
	
	

	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,getElement: function(key){return this.view.getElement(key);}
	,isNeverBeDrawn: function(){return this.view.isNeverBeDrawn();}
	,preparePattern: function(){this.view.preparePattern();}
	,display: function(){this.view.display();}
	,isHidden: function(){return this.view.isHidden();}
	,hide: function(){this.view.hide();}
	,set: function(key,value){this.view.set(key,value);}
	,getString: function(key,defValue){return this.view.getString(key,defValue);}
	,getInt: function(key,defValue){return this.view.getInt(key,defValue);}
	,getBoolean: function(key,defValue){return this.view.getBoolean(key,defValue);}
	,addEvent: function(key,eventName,fnHandler,bCapture){ihelpuoo.widget.addViewEventByKey(this,key,eventName,fnHandler,bCapture);}
	// </WEBOO:INHERIT>	
}